<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" @click="handleAdd">Add</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Name">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="XH">
        <template slot-scope="scope">
          {{ scope.row.xh }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="SFZ">
        <template slot-scope="scope">
          {{ scope.row.sfz }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Email">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Phone">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Address">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="Actions" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="warning" size="mini" @click="handleEdit(row)">Edit</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row, $index)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- Edit dialog -->
    <el-dialog title="Edit" :visible.sync="dialogVisible">
      <el-form :model="tempForm">
        <el-form-item label="Name" prop="name">
          <el-input v-model="tempForm.name" />
        </el-form-item>
        <el-form-item label="XH" prop="xh">
          <el-input v-model="tempForm.xh" />
        </el-form-item>
        <el-form-item label="SFZ" prop="sfz">
          <el-input v-model="tempForm.sfz" />
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="tempForm.email" />
        </el-form-item>
        <el-form-item label="Phone" prop="phone">
          <el-input v-model="tempForm.phone" />
        </el-form-item>
        <el-form-item label="Address" prop="address">
          <el-input v-model="tempForm.address" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="dialogStatus === 'add' ? addData():updateData()">提交</el-button>
        <el-button type="danger" @click="handleCancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList, updateInfo, addInfo } from '@/api/student'
export default {
  data() {
    return {
      // Store table data, recieve data from backend
      list: null,
      // Identify list loading status
      listLoading: true,
      // Store temp form data
      tempForm: {
        id: null,
        name: '',
        xh: null,
        sfz: null,
        email: '',
        phone: '',
        address: ''
      },
      dialogVisible: false,
      // Is edit or add?
      dialogStatus: ''
    }
  },
  created() {
    // Get request data during created period
    this.fetchData()
  },
  methods: {
    // Send request and fetch data from backend
    fetchData() {
      this.listLoading = true
      getList().then(res => {
        this.list = res.data.items
        this.listLoading = false
      })
    },
    // Edit selected row data
    handleEdit(row) {
      this.tempForm = Object.assign({}, row)
      this.dialogVisible = true
    },
    // Update data
    updateData() {
      // Send post request, and update data both in page and database
      const tempData = Object.assign({}, this.tempForm)
      updateInfo(tempData).then(res => {
        const index = this.list.findIndex(v => v.id === this.tempForm.id)
        // Replace list data in list according to index
        this.list.splice(index, 1, this.tempForm)
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
      })
    },
    // Delete specific data from list
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      // Delete data in page
      this.list.splice(index, 1)
      // Send request and delete data in database
    },
    // Cancle update
    handleCancel() {
      this.tempForm = {}
      this.dialogVisible = false
    },
    resetTempForm() {
      this.tempForm = {
        id: null,
        name: '',
        xh: null,
        sfz: null,
        email: '',
        phone: '',
        address: ''
      }
    },
    // Add record
    handleAdd() {
      // Reset tempFrom
      this.resetTempForm()
      this.dialogStatus = 'add'
      this.dialogVisible = true
    },
    addData() {
      this.tempForm.id = parseInt(Math.random() * 100) + 1024
      addInfo(this.tempForm).then(res => {
        // Insert a new record at the begining of list
        this.list.unshift(this.tempForm)
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: 'Add Successfully',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
